﻿using ControlHelper.Attributes;
using Creator.Access.Core;
using Creator.Core.Base;
using Creator.Core.Entity;
using System.Collections.Generic;
using System.Linq;

namespace Creator.Core.Component
{

    /// <summary>
    /// 监控物流线组件
    /// </summary>
    [ComponentMenu("处理/物流线监控")]
    public class GroupBeltComponet : LogicComponet
    {
        public GroupBeltComponet(BaseObject baseObject) : base(baseObject)
        {
        }
        public class WorkItem
        {
            public string WorkId { get; set; }
            public string WorkName { get; set; }
        }
        [NickName("描述1234")]
        public string Description { get; set; }

        [NickName("Rgv数据库连接", "当达到条件之后，更新的数据库")]
        public OperationComponent DBOpreation { get; set; }

        [NickName("物流线数据","编辑了工位编号后，用工位编号从这个数据里面去进行筛选")]
        public DataListComponent DataList { get; set; }


        [NickName("工位编号", "需要监视的工位编号有哪些")]
        [DataGrid]
        public List<WorkItem> Workids { get; set; } = new List<WorkItem>();

        [NickName("空盘最小数量","当空盘数量低于多少时，则触发要空盘")]
        public int EmptyMinLimit { get; set; }
        [NickName("满盘最小数量", "当满盘数量低于多少时，则触发要满盘")]
        public int FullMinLimit { get; set; }
        


        [NickName("满盘盘状态")]
        public int FullTrayStatus { get; set; }
        [NickName("空盘盘状态")]
        public int EmptyTrayStatus { get; set; }

        [NickName("满盘取放类型")]
        public int FullGetputType { get; set; }
        [NickName("空盘取放类型")]
        public int EmptyGetputType { get; set; }

        [NickName("放盘口ID","当达到条件之后，需要更新的取放口id，多个ID用','隔开")]
        public string PutFloorId { get; set; }

        /// <summary>
        /// 从源数据筛选需要监控的物流线
        /// </summary>
        internal List<belt_floor_table> Belts;
         

        public override void Run(double deltaTime)
        { 
            if (DataList == null || DataList.Data == null || DataList.Data.Count == 0)
            {
                //todo:记录 错误
                return;
            }

      
            if (DataList.Data is List<belt_floor_table> datas)
            {
                Belts = datas.Where(a => Workids.Select(x=> x.WorkId).Contains(a.work_id)).ToList();
            } 
            if (Belts == null || Belts.Count == 0) return;

            //获取这个区间 满盘数量 和空盘数量
            var fullCount = Belts.Where(a => a.load_staus > FullTrayStatus).Count();
            var emptyCount = Belts.Where(a => a.load_staus == EmptyTrayStatus).Count();

            //todo:当取盘放盘的时候就等待 不允许去更改


            if (fullCount <= FullMinLimit)
            {
                //满盘数量少于设定值, 更新要满盘
                UpdateFlootGetputType(DBOpreation.Operation,PutFloorId, FullGetputType);
            }

            if(emptyCount <= EmptyMinLimit)
            {
                //空盘数量少于设定值， 更新要空盘
                UpdateFlootGetputType(DBOpreation.Operation, PutFloorId, EmptyGetputType);

            } 
        }
        #region 数据库更新

        /// <summary>
        /// 更新对应层id的取放类型
        /// </summary>
        /// <param name="floorId">层id 见：<see cref="GroupBeltComponet.PutFloorId"/></param>
        /// <param name="getputType">取放类型</param>
        /// <returns></returns>
        internal bool UpdateFlootGetputType(OperationBase Operation, string floorId, int getputType)
        {
            if (Operation == null)
            {
                //todo:记录错误，没有配置连接方式
                return false;
            }
            if (!(Operation is DataBaseOperation dbOperation))
            {
                //todo:记录错误，
                return false;
            }
            var ids = floorId.Split(',');
            string sql = $"update sys_floot_table2 set get_put_Type ={getputType} ";
            foreach (var id in ids)
            {
                //todo:组合sql语句



            }

            return dbOperation.db.ExecNonQuery("") > 0;
        }

        internal bool CheckCanUpdate()
        {

            return false;
        }

        #endregion

        public override string Name => "物流线分组数据";
    }


  
}
